
#ifndef __APP_UPDATE_H_
#define	__APP_UPDATE_H_

#include <stdint.h>
#include "dataproc.h"
#include "rtctime.h"
#include "mytype.h"

#ifdef	__cplusplus
extern "C" {
#endif
	
#ifdef   _APP_UPDATE_MODULE_
#define  MY_EXT
#else
#define  MY_EXT  extern
#endif  

#define FIL_OPEN_R      1  //只读，没有则打开失败 
#define FIL_OPEN_W      2  //只写，没有则新建 
#define FIL_OPEN_WA     4  //只写，始终新建 


struct updata_ctr_str
{
    union
    {
        uint8_t st;
        struct
        {
            uint8_t err_crc : 1;
            uint8_t err_no_file : 1;
            uint8_t err_file_read : 1;
            uint8_t err_sum : 1;
            uint8_t err_size : 1;
            uint8_t id_err : 1;
        } bits;
    } err;
    
    union
    {
        uint8_t st;
        struct
        {
            uint8_t app_same : 1;
            uint8_t update_req : 1;
            uint8_t no_update : 1;
            uint8_t update_ok : 1;
            uint8_t update_err : 1;
        } bits;
    } out;

    uint32_t enc_file_size;
    uint32_t read_len;
    uint32_t len_back;
    uint8_t *buff;
    uint8_t *bk_buff;
    uint32_t sum[4];
    uint16_t crc[4];
    uint16_t crc_src;
    uint16_t crc_des;
};

MY_EXT struct updata_ctr_str update_ctr;

/*  */
void app_update(uint8_t check0_or_program1);

void ap_cfg_ext(void);

/* hal opt */
uint8_t ap_flash_read(uint32_t addr, uint32_t len, uint8_t *pdat);
uint8_t ap_flash_write(uint32_t addr, uint32_t len, uint8_t *pdat);
uint8_t ap_flash_erase(uint32_t addr);

/* file opt */
uint16_t ap_file_open(const char * file_name, uint8_t mode);
uint16_t ap_file_read(void * buff, uint16_t len);
uint16_t ap_file_write(void * buff, uint16_t len);
uint16_t ap_file_write_sync(void);
uint32_t ap_file_seek_end(void);
uint32_t ap_file_size_get(void);
uint16_t ap_file_close(void);

#undef MY_EXT

#ifdef	__cplusplus
}
#endif

#endif	/* __APP_UPDATE_H_ */
